import React from "react"
import { useSelector } from "react-redux"

const AuthRouter = (props: { children: JSX.Element }) => {
    const authList=useSelector((store:any)=>store.counter.sidebarList)
    const whiteList: string[] = ["login"]
    const route:any=window.location.href.split('/').pop()?.split('?'||'/')[0]
    let token = localStorage.getItem("token")
    

    // 判断是否为白名单路由
    if (whiteList.includes(route)) {
      return props.children;
    }

    if (!token) {
      return window.location.href = '/login'
    }else{
      if(authList.length>0){
        if (!authList.includes(route)) {
          return (
            <div>
              <h1>无权访问该页面</h1>
            </div>
          )
        }
      }
      
    }

  // 返回子组件
  return props.children;
}

export default AuthRouter


// src/components/AuthRouter.tsx

// import React from 'react';
// import Login from './login/Login';
// import useAuth from '../components/AuthContext';

// const AuthRouter: React.FC<{ children: JSX.Element }> = ({ children }) => {
//   const auth = useAuth();
//   const token=localStorage.getItem("token")
//   // 如果用户未认证，则重定向到登录页
//   if (token) {
//     return <Login></Login>
//   }

//   return children;
// };

// export default AuthRouter;